<?php
class WBaseHelper extends USystemWorklet
{
	private $_keyPrefix;
	
	/**
	 * Saves data into an application cookie.
	 * @param string item name
	 * @param mixed item value
	 */
	public function taskSaveToCookie($name,$value)
	{
		if($name == 'location' && !$value)
			return;
		
		$cookie=app()->getRequest()->getCookies()->itemAt($this->getStateKeyPrefix());
		if(!$cookie)
		{
			$cookie = new CHttpCookie($this->getStateKeyPrefix(),'');
			$data = array();
		}
		else
			$data = unserialize($cookie->value);
		$cookie->expire = time()+31536000;
		$data[$name] = $value;
		$cookie->value = serialize($data);
		app()->getRequest()->getCookies()->add($cookie->name,$cookie);
	}
	
	/**
	 * Retrieves data item from the application cookie.
	 * @param string item name
	 * @return mixed item value
	 */
	public function taskGetFromCookie($name)
	{
		$cookie=app()->getRequest()->getCookies()->itemAt($this->getStateKeyPrefix());
		if($cookie)
		{
			$data = unserialize($cookie->value);
			return isset($data[$name])?$data[$name]:null;
		}
		return null;
	}
	
	public function taskIsMobile($ignoreSetting=false)
	{
		if(!$ignoreSetting)
		{
			$setting = $this->getFromCookie('ignoreMobile');
			if($setting)
				return false;
		}
		return app()->request->isMobile;
	}
	
	public function getStateKeyPrefix()
	{
		if($this->_keyPrefix!==null)
			return $this->_keyPrefix;
		else
			return $this->_keyPrefix=md5('UniProgy.'.get_class($this).'.'.Yii::app()->getId());
	}
	
	public function setStateKeyPrefix($value)
	{
		$this->_keyPrefix=$value;
	}
}